package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.MyFollowAdapter;
import tongwentongshu.com.app.bean.MyFollowBean;
import tongwentongshu.com.app.contract.MyFollowContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.presenter.MyFollowPresenter;

/**
 * Created by XiongBin on 2017/7/18.
 */

public class MyFollowActivity extends BaseActivity implements MyFollowContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private MyFollowAdapter myFollowAdapter;
    protected boolean onRefreshJudge = false;
    private MyFollowPresenter myFollowPresenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_my_follow;
    }

    @Override
    protected int getTitleId() {
        return R.string.my_follow;
    }

    @Override
    protected void initView() {
        onRefreshJudge = true;
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        List arr = new ArrayList<MyFollowBean.DataBean>();
        myFollowAdapter = new MyFollowAdapter(mContext, arr);
        myFollowAdapter.setOnLoadMoreListener(this, mRecyclerView);
        myFollowAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setAdapter(myFollowAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                showUser(msg.getData().get(position).getId());
            }
        });
    }

    void showUser(String userId) {
        Bundle b = new Bundle();
        b.putString("phone", "");
        b.putString(Cache.USER_ID, userId);
        startActivity(PersonalHomePageActivity.class, b);
    }

    @Override
    protected void initData() {
        myFollowPresenter = new MyFollowPresenter(this);
        myFollowPresenter.getMyFollow(mContext);
    }

    MyFollowBean msg;

    @Override
    public void onSuccess(MyFollowBean msg) {
        this.msg = msg;
        View empty = getLayoutInflater().inflate(R.layout.empty_no_data, (ViewGroup) mRecyclerView.getParent(), false);
        //pullToRefreshAdapter.setPreLoadNumber(3);
        myFollowAdapter.setEmptyView(empty);
        if (msg.getData() != null && msg.getData().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                myFollowAdapter.setNewData(msg.getData());
                mSwipeRefreshLayout.setRefreshing(false);
                myFollowAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            myFollowAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }

    @Override
    public void onShowError(String msg) {

    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
        myFollowPresenter.getMyFollow(mContext);
    }

    @Override
    public void onLoadMoreRequested() {
        myFollowAdapter.loadMoreEnd(true);
    }
}
